import * as types from './mutation-types'
import { setStorage, removeStorage } from '@/utils'

const mutations = {
  // 登陆
  [types.SET_LOGIN](state) {
    state.hasLogin = true
    setStorage('hasLogin', true, 0)
  },
  // 登出
  [types.SET_LOGOUT](state) {
    state.hasLogin = false
    state.refreshToken = ''
    state.token = ''
    removeStorage('hasLogin')
    removeStorage('refreshToken')
    removeStorage('token')
    // 清除个人信息
    this.commit({
      type: 'user/REMOVE_USER_INFO'
    })
  },
  // 设置refreshToken
  [types.SET_REFRESHTOKEN](state, refreshToken) {
    state.refreshToken = refreshToken
    setStorage('refreshToken', refreshToken, 0)
  },
  // 设置token
  [types.SET_TOKEN](state, token) {
    state.token = token
    setStorage('token', token)
  }
}

export default mutations
